﻿using System;
using System.Windows.Forms;

namespace TextEditor
{
    

    public class A_InsertImage : A_Action
    {
        public override string ActionName()
        {
            return "insertimage";
        }

        public override A_Action Clone()
        {
            A_InsertImage image = new A_InsertImage();
            image.BaseCloneFrom(this);
            return image;
        }

        public override bool Execute()
        {
            ZYTextImage newElement = new ZYTextImage();
            newElement.Src = base.Param1;
            newElement.ImageType = base.Param2;
            newElement.Image = ZYTextConst.ImageFromURL(base.Param1);
            newElement.SaveInFile = true;
            if (!(base.myOwnerDocument.Content.PreElement is ZYTextParagraph))
            {

                base.myOwnerDocument.Content.InsertElement(new ZYTextParagraph());
            }
            base.myOwnerDocument._InsertElement(newElement);
            return true;
        }

        public override bool isEnable()
        {
            return base.myOwnerDocument.CanModify();
        }

        public override bool UIExecute()
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Filter = "图像文件 (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|所有文件 (*.*)|*.*";
                dialog.Title = "插入图片";
                dialog.ShowReadOnly = false;
                dialog.ShowHelp = false;
                dialog.CheckFileExists = true;
                dialog.ReadOnlyChecked = false;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    base.Param1 = dialog.FileName;
                    base.Param2 = "";
                    return this.Execute();
                }
            }
            return false;
        }
    }
}

